package javolution.io;

import java.io.IOException;
import java.io.Reader;
import javolution.lang.MathLib;
import javolution.lang.Reusable;
import javolution.text.CharArray;
import javolution.text.Text;
import javolution.text.TextBuilder;

/* loaded from: classes2.dex */
public final class CharSequenceReader extends Reader implements Reusable {
    private int _index;
    private CharSequence _input;

    @Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this._input != null) {
            reset();
        }
    }

    @Override // java.io.Reader
    public int read() throws IOException {
        CharSequence charSequence = this._input;
        if (charSequence != null) {
            if (this._index < charSequence.length()) {
                CharSequence charSequence2 = this._input;
                int i = this._index;
                this._index = i + 1;
                return charSequence2.charAt(i);
            }
            return -1;
        }
        throw new IOException("Reader closed");
    }

    @Override // java.io.Reader
    public int read(char[] cArr, int i, int i2) throws IOException {
        CharSequence charSequence = this._input;
        if (charSequence != null) {
            int length = charSequence.length();
            int i3 = this._index;
            if (i3 >= length) {
                return -1;
            }
            int min = MathLib.min(length - i3, i2);
            CharSequence charSequence2 = this._input;
            if (charSequence2 instanceof String) {
                int i4 = this._index;
                ((String) charSequence2).getChars(i4, i4 + min, cArr, i);
            } else if (charSequence2 instanceof Text) {
                int i5 = this._index;
                ((Text) charSequence2).getChars(i5, i5 + min, cArr, i);
            } else if (charSequence2 instanceof TextBuilder) {
                int i6 = this._index;
                ((TextBuilder) charSequence2).getChars(i6, i6 + min, cArr, i);
            } else if (charSequence2 instanceof CharArray) {
                CharArray charArray = (CharArray) charSequence2;
                System.arraycopy(charArray.array(), this._index + charArray.offset(), cArr, i, min);
            } else {
                int i7 = i + min;
                int i8 = this._index;
                while (i < i7) {
                    cArr[i] = this._input.charAt(i8);
                    i++;
                    i8++;
                }
            }
            this._index += min;
            return min;
        }
        throw new IOException("Reader closed");
    }

    public void read(Appendable appendable) throws IOException {
        CharSequence charSequence = this._input;
        if (charSequence == null) {
            throw new IOException("Reader closed");
        }
        appendable.append(charSequence);
    }

    @Override // java.io.Reader
    public boolean ready() throws IOException {
        if (this._input != null) {
            return true;
        }
        throw new IOException("Reader closed");
    }

    @Override // java.io.Reader, javolution.lang.Reusable
    public void reset() {
        this._index = 0;
        this._input = null;
    }

    public CharSequenceReader setInput(CharSequence charSequence) {
        if (this._input == null) {
            this._input = charSequence;
            return this;
        }
        throw new IllegalStateException("Reader not closed or reset");
    }
}
